package com.hdw.med.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hdw.common.base.BaseException;
import com.hdw.common.base.mapper.BaseMapper;
import com.hdw.common.base.mapper.BaseMysqlMapper;
import com.hdw.common.base.mapper.BaseOracleMapper;
import com.hdw.common.db.RoutingDataSource;
import com.hdw.common.db.ThreadLocalDataSource;
import com.hdw.common.vo.FieldAttrVO;
import com.hdw.common.vo.PaginationVO;
import com.hdw.med.api.TableOperService;
import com.hdw.med.bean.model.BaseDb;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by liujunlei on 2021/5/28.
 */
@Service
public class TableOperServiceImpl implements TableOperService {

    @Autowired
    BaseMysqlMapper baseMysqlMapper;
    @Autowired
    BaseOracleMapper baseOracleMapper;
    @Autowired
    BaseMapper baseMapper;

    @Override
    public PaginationVO<Map<String, Object>> findByDbAndSql(String sql, Long idDb, Integer pageNum, Integer pageSize) throws BaseException {
        PaginationVO<Map<String, Object>> paginationVo = null;
        try {
            PageHelper.startPage(pageNum == null ? 1 : pageNum, pageSize == null ? 10 : pageSize);
            List<Map<String, Object>> quesList = baseMapper.findBySql(sql);
            PageInfo<Map<String, Object>> pageInfo = new PageInfo<>(quesList);
            paginationVo = new PaginationVO(pageInfo.getTotal(), pageInfo.getList());
        } catch (Exception e) {
            throw new BaseException("表不存在无法查询");
        }
        return paginationVo;
    }

    @Override
    @RoutingDataSource
    public List<FieldAttrVO> findFieldMsgFromDb(BaseDb baseDb, String tableName) {
        ThreadLocalDataSource.setLocalSource(String.valueOf(baseDb.getIdDb()));
        List<FieldAttrVO> list;
        if ("oracle".equals(baseDb.getEuTp())) {
            list = baseOracleMapper.findFieldsByTable(tableName, baseDb.getUsename().toUpperCase());
        } else {
            list = baseMysqlMapper.findFieldsByTable(tableName, baseDb.getTbSchema());
        }
        return list;
    }

    @Override
    @RoutingDataSource
    public int execDdlSqlByDb(String sql, String idDb) {
        ThreadLocalDataSource.setLocalSource(idDb);
        return baseMapper.execDdlSql(sql);
    }

    @Override
    @RoutingDataSource
    public int execDelByDb(String sql, String idDb) {
        ThreadLocalDataSource.setLocalSource(idDb);
        return baseMapper.execDelSql(sql);
    }

    @Override
    @RoutingDataSource
    public List<FieldAttrVO> findTables(BaseDb baseDb,String tableName) {
        ThreadLocalDataSource.setLocalSource(String.valueOf(baseDb.getIdDb()));
        List<FieldAttrVO> list = new ArrayList();
        switch (baseDb.getEuTp()) {
            case "mysql":
                list = baseMysqlMapper.findTables(tableName,baseDb.getTbSchema());
                break;
            case "oracle":
                list = baseOracleMapper.findTables(tableName,baseDb.getTbSchema().toUpperCase());
                break;
        }
        return list;
    }

}
